import pandas as pd
from wordcloud import WordCloud
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

data = pd.read_csv('personal_history/清理后的个人历史数据（非直播）.csv', parse_dates=['时间'])

# ===============
#  1、标签分析
# ===============
labels = data['标签'].sum().split(' ')
labels_dict = {}
for label in labels:
    labels_dict[label] = labels_dict.get(label, 0) + 1
background = np.array(Image.open('background.jpeg'))
word_cloud = WordCloud(
    font_path="simhei.ttf",
    width=1200,                   #词云图宽
    height=1000,                  #词云图高
    background_color='white',    #词云图背景颜色
    mask=background,
    max_words= 80,
    max_font_size= 70,
    random_state=1).fit_words(labels_dict)     #词云图的参考图
plt.figure(figsize=(8,8))
plt.imshow(word_cloud)
plt.axis('off')
# plt.show()

# =============
#  2、时间分析
# =============
plt.rcParams['font.sans-serif']=['SimHei']
plt.figure(figsize=(8,6))
data['时间'].apply(lambda x : x.date()).value_counts().plot()
plt.title('每日浏览次数统计')
plt.grid()
# plt.show()

# =============
#  3、时间+点赞
# =============
data['day'] = data['时间'].apply(lambda x : x.date())
plt.figure(figsize=(10,8))
plt.plot(data[['点赞数','day']].groupby('day').mean(), 'red', label='Average')
plt.title('每日浏览视频的平均点赞数')
plt.legend()
plt.grid()
plt.show()
